/********异步提交函数**************************/
//form支持多表单提交，中间用,隔开，url为提交地址
function Sub(form,url){
	//串联表单
	var formName = form.split(",");
	var serialize = "";
	var a = "";
	for(var i=0;i<formName.length;i++){
        if(serialize == ""){
		   a = "";
		}else{
		   a = "&";
		}
		serialize += a + $("[name="+formName[i]+"]").serialize();
	}
	//异步提交数据
	$.post(url,serialize,function(data){
		//console.log(data);
		if(data.warn == 2){
			if(data.href){//如果异步返回的json参数中定义了重定向url，则跳转到本url
				window.location.href = data.href;
			}else{
				window.location.reload();
			}
		}else{
			warn(data.warn);
		}
	},"json");
}
/******************警示弹出层*****************/
function warn(warn){
	
	$("#warn").show();
	$("#warnWord").html(warn);
}
/******************选择省份，区域，城市*****************/
function region(FormName,Formprovince,Formcity,Formarea){
    var province = eval('document.' + FormName + "." + Formprovince);
    var city = eval('document.' + FormName + "." + Formcity);
    var area = eval('document.' + FormName + "." + Formarea);
	//根据省份返回城市
	province.onchange = function(){
	    area.innerHTML = "<option value=''>--区域--</option>";
		$.post(root+"library/libData.php",{ProvincePostCity:this.value},function(data){
		    city.innerHTML = data.city;
		},"json");
	}
	//根据省份返回城市
	city.onchange = function(){
	    $.post(root+"library/libData.php",{ProvincePostArea:province.value,CityPostArea:this.value},function(data){
		    area.innerHTML = data.area;
		},"json");
	}
}
/******************选择商品分类*****************/
function goodsType(formName,formOne,formTwo,formThree){
    var one = eval('document.' + formName + "." + formOne);
    var two = eval('document.' + formName + "." + formTwo);
    var three = eval('document.' + formName + "." + formThree);
	//根据产品一级分类返回二级分类
	one.onchange = function(){
	    three.innerHTML = "<option value=''>--三级分类--</option>";
		$.post(root+"library/libData.php?type=getGoodsTwo",{oneId:this.value},function(data){
		    two.innerHTML = data.two;
		},"json");
	}
	//根据产品二级分类返回三级分类
	two.onchange = function(){
	    $.post(root+"library/libData.php?type=getGoodsThree",{twoId:this.value},function(data){
		    three.innerHTML = data.three;
		},"json");
	}
}
/*******获取本地路径*****************/
//sourceId：文件域ID号
function getFileUrl(sourceId){
	var url;
	if(navigator.userAgent.indexOf("MSIE")>=1) { // IE
		url = document.getElementById(sourceId).value;
	}else if(navigator.userAgent.indexOf("Firefox")>0) { // Firefox
		url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
	}else if(navigator.userAgent.indexOf("Chrome")>0) { // Chrome
		url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
	}else{
		url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
	}
	return url;
}
/*******获取短信验证码*****************/
function prove(tel){
	$.post(root+"library/libData.php?type=RegisterCheckTel",{tel:tel},function(data){
		warn(data.warn);
	},"json");
}
/*******收藏商品*****************/
function goodsCollect(goodsId){
	$.post(root+"library/usData.php?type=collectGoods",{goodsId:goodsId},function(data){
	    warn(data.warn);
	},"json");
}
/*******商品规格选择*****************/
function skuChoice(id,stock,price){
    $("#goodsPrice").text("￥"+price);
	$("#goodsSkuStock").text(stock);
	$("#buyCarNum").attr("goodsSkuId",id);
}
/*******改变购买数量*****************/
function editBuyNum(type){
	var num = $("#buyCarNum").val();
	var maxNum = parseFloat($("#goodsSkuStock").text());
	var goodsSkuId = $("#buyCarNum").attr("goodsSkuId");
	if(goodsSkuId == ""){
		warn("请选择商品规格");
	}else if(maxNum == 0){
		warn("库存为零");
	}else if(type == "cut"){
		if(num <= 1){
			warn("不能再少了");
		}else{
			num--;
		}
	}else if(type == "add"){
		if(num >= maxNum){
			warn("购买量不能超过最大库存");
		}else{
			num++;
		}
	}
	$("#buyCarNum").val(num);
}
/*******加入购物车或立即购买*****************/
function addBuyCar(type){
	$.post(root+"library/usData.php?type=addBuyCar",{buyCarNum:$("#buyCarNum").val(),goodsSkuId:$("#buyCarNum").attr("goodsSkuId")},function(data){
		if(data.warn == 2){
			if(type == "atOnce"){
				window.location.href = data.href;
			}else{
				$("#buyCarMenu").text(data.sum);
				warn("加入购物车成功");
			}
		}else{
			warn(data.warn);
		}
	},"json");
}
//商品选择状态切换函数
function buyCarSwitch(id,obj){
	$.post(root+"library/usData.php?type=buyCarSwitch",{buyCarId:id},function(data){
		if(data.warn == 2){
			$("#allMoney").text("￥" + data.money);
			$("#buyCarSum").text(data.sum);
			$("#payId").text(data.pay);
			if(data.page == "page"){
				goodsMenuNum(data.sum);
			}
			if(id == "allCheck"){
				if(data.checkAll == "checked"){
					$("[switchBuyCar]").prop("checked",true);
				}else{
					$("[switchBuyCar]").prop("checked",false);
				}
			}else{
				if(data.buyCarChecked == "yes"){
					$("[switchBuyCar="+id+"]").prop('checked',true);
				}else{
					$("[switchBuyCar="+id+"]").prop('checked',false);
				}
				if(data.checkAll == "checked"){
					$("[switchBuyCar=allCheck]").prop('checked',true);
				}else{
					$("[switchBuyCar=allCheck]").prop('checked',false);
				}
			}
		}
	},"json");
}
//商品数量增减函数
function buyCarNum(id,type){
	$.post(root+"library/usData.php?type=buyCarNum",{buyCarId:id,direction:type},function(data){
		if(data.warn == 2){
			$("#allMoney").text("￥" + data.money);
			$("#buyCarSum").text(data.sum);
			$("[numBuyCarId="+id+"]").val(data.goodsNum);
			$("[singlePrice="+id+"]").text("￥" + data.singlePrice);
			$("#payId").text(data.pay);
			if(data.page == "page"){
				goodsMenuNum(data.sum);
			}
		}else{
			warn(data.warn);
		}
	},"json");
}
//购物车商标放大缩小效果函数
function goodsMenuNum(sum) {
	$("#buyCarMenu").text(sum);
	$("#buyCarMenu").animate({
		height: '24px',
		lineHeight: '24px',
		width: '24px',
		top: '-10px',
		left: '101px'
	}, 100);
	$("#buyCarMenu").animate({
		height: '14px',
		lineHeight: '14px',
		width: '20px',
		top: '-1px',
		left: '94px'
	}, 100)
}
//结算函数
function buyCarPay(){
	$.post(root+"library/usData.php?type=usBuyCarPay",function(data){
		if(data.warn == 1){
			document.payForm.submit();
		}else if(data.warn == 2){
			window.location.href = data.href;
		}else{
			warn(data.warn);
		}
	},"json");
}
//客户订单执行指令函数
function editOrder(type,id){
	$.post(root+"library/usData.php?type=usEditOrder",{type:type,id:id},function(data){
		if(data.warn == 2){
		    window.location.reload();
		}else{
			warn(data.warn);
		}
	},"json");
}
